package com.izocen.rules.core.factory;

import com.izocen.rules.assembler.ExpressAssembler;
import com.izocen.rules.common.ResponseMessage;
import com.izocen.rules.common.RuleEntity;
import com.izocen.rules.core.rule.RuleUnit;

/**规则工厂
 * @author pengyongjun
 *
 */
public class RuleUnitFactory {
	
	/**
	 * 规则组装器
	 */
	public static ExpressAssembler ea = ExpressAssembler.getInstance();
	
	public static ResponseMessage<RuleUnit> create(RuleEntity re) {
		RuleUnit ru = new RuleUnit();
		ru.setId(re.getId());
		ru.setGroupId(re.getGroupId());
		String express = re.getExpress();
		ResponseMessage<String> rm = ea.assemble(express);
		if(rm.getCode() == ResponseMessage.FAIL) {
			return ResponseMessage.fail(rm.getMsg());
		}
		ru.setExpress(rm.getData());
		return ResponseMessage.succ(ru);
	} 
}
